#!/usr/local/php70/bin/php
<?php
if($argc<2) usage();

$zts = $world = false;
$build = [];
$conf = [];

$version = preg_replace('/[\.\-]+/', '', $argv[1]);

function usage() {
    $cmd = basename(__FILE__);
    echo "Usage: $cmd 71|70|56|55|54|53|all|world [zts]\n";
    echo "To rebuild PHP 7.0 and 7.0-debug: $cmd 70\n";
    echo "To rebuild PHP 7.0 and 7.0-debug threadsafe: $cmd 70 zts\n";
    echo "To rebuild PHP 5.*: $cmd 5\n";
    exit;
}

if(!empty($argv[2]) && $argv[2]=='zts') $zts = true;

switch($argv[1]) {
    case '71': $build[71]=true; break;
    case '7':
    case '70': $build[7]=true; break;
    case '56': $build[56]=true; break;
    case '55': $build[55]=true; break;
    case '54': $build[54]=true; break;
    case '5': $build = [56=>true,55=>true,54=>true]; break;
    case 'all': $build = [71=>true, 7=>true, 56=>true,55=>true,54=>true]; break;
    case 'world': $build = [71=>true, 7=>true, 56=>true,55=>true,54=>true]; $world=true; break;
}

$conf[71] = [
'--with-apxs2=/usr/bin/apxs2',
'--enable-zend-signals',
'--with-gd',
'--with-jpeg-dir=/usr',
'--with-png-dir=/usr',
'--with-vpx-dir=/usr',
'--with-freetype-dir=/usr',
'--with-t1lib=/usr',
'--enable-gd-native-ttf',
'--enable-exif',
'--with-config-file-path=/etc/php71',
'--with-config-file-scan-dir=/etc/php71/conf.d',
'--with-mysql-sock=/var/run/mysqld/mysqld.sock',
'--with-zlib',
'--enable-phpdbg',
'--with-gmp',
'--with-zlib-dir=/usr',
'--with-gettext',
'--with-kerberos',
'--with-imap-ssl',
'--with-mcrypt=/usr/local',
'--with-iconv',
'--enable-sockets',
'--with-openssl',
'--with-pspell',
'--with-pdo-mysql=mysqlnd',
'--with-pdo-sqlite',
'--with-pgsql',
'--with-pdo-pgsql',
'--enable-soap',
'--enable-xmlreader',
'--enable-phar=shared',
'--with-xsl',
'--enable-ftp',
'--enable-cgi',
'--with-curl=/usr',
'--with-tidy',
'--with-xmlrpc',
'--enable-mbstring',
'--enable-sysvsem',
'--enable-sysvshm',
'--enable-shmop',
'--with-readline',
'--enable-pcntl',
'--enable-fpm',
'--with-fpm-systemd',
'--enable-intl',
'--enable-zip',
'--with-imap',
'--with-mysqli=mysqlnd',
'--enable-calendar',
'--enable-bcmath',
'--enable-opcache-file'
];

$conf[7] = [ 
'--with-apxs2=/usr/bin/apxs2',
'--enable-zend-signals',
'--with-gd',
'--with-jpeg-dir=/usr',
'--with-png-dir=/usr',
'--with-vpx-dir=/usr',
'--with-freetype-dir=/usr',
'--with-t1lib=/usr',
'--enable-gd-native-ttf',
'--enable-exif',
'--with-config-file-path=/etc/php7',
'--with-config-file-scan-dir=/etc/php7/conf.d',
'--with-mysql-sock=/var/run/mysqld/mysqld.sock',
'--with-zlib',
'--enable-phpdbg',
'--with-gmp',
'--with-zlib-dir=/usr',
'--with-gettext',
'--with-kerberos',
'--with-imap-ssl',
'--with-mcrypt=/usr/local',
'--with-iconv',
'--enable-sockets',
'--with-openssl',
'--with-pspell',
'--with-pdo-mysql=mysqlnd',
'--with-pdo-sqlite',
'--with-pgsql',
'--with-pdo-pgsql',
'--enable-soap',
'--enable-xmlreader',
'--enable-phar=shared',
'--with-xsl',
'--enable-ftp',
'--enable-cgi',
'--with-curl=/usr',
'--with-tidy',
'--with-xmlrpc',
'--enable-mbstring',
'--enable-sysvsem',
'--enable-sysvshm',
'--enable-shmop',
'--with-readline',
'--enable-pcntl',
'--enable-fpm',
'--with-fpm-systemd',
'--enable-intl',
'--enable-zip',
'--with-imap',
'--with-mysqli=mysqlnd',
'--enable-calendar',
'--enable-bcmath',
'--enable-opcache-file'
];

$conf[56] = [
'--with-apxs2=/usr/bin/apxs2',
'--enable-zend-signals',
'--with-gd',
'--with-jpeg-dir=/usr',
'--with-png-dir=/usr',
'--with-vpx-dir=/usr',
'--with-freetype-dir=/usr',
'--with-t1lib=/usr',
'--enable-gd-native-ttf',
'--enable-exif',
'--with-config-file-path=/etc/php56',
'--with-config-file-scan-dir=/etc/php56/conf.d',
'--with-mysql=/usr',
'--with-mysql-sock=/var/run/mysqld/mysqld.sock',
'--with-zlib',
'--with-gmp',
'--with-zlib-dir=/usr',
'--with-gettext',
'--with-kerberos',
'--with-imap-ssl',
'--with-mcrypt=/usr/local',
'--with-iconv',
'--enable-sockets',
'--with-openssl',
'--with-pspell',
'--with-pdo-mysql=mysqlnd',
'--with-pdo-sqlite',
'--with-pgsql',
'--with-pdo-pgsql',
'--enable-soap',
'--enable-zip',
'--enable-xmlreader',
'--enable-phar=shared',
'--with-xsl',
'--enable-ftp',
'--enable-cgi',
'--with-curl=/usr',
'--with-tidy',
'--with-xmlrpc',
'--enable-mbstring',
'--enable-sysvsem',
'--enable-sysvshm',
'--enable-shmop',
'--with-readline',
'--enable-pcntl',
'--enable-fpm',
'--enable-intl',
'--with-imap',
'--with-mysqli=mysqlnd',
'--enable-calendar',
'--enable-bcmath'
];

$conf[55] = [
'--with-apxs2=/usr/bin/apxs2',
'--enable-zend-signals',
'--with-gd',
'--with-jpeg-dir=/usr',
'--with-png-dir=/usr',
'--with-vpx-dir=/usr',
'--with-freetype-dir=/usr',
'--with-t1lib=/usr',
'--enable-gd-native-ttf',
'--enable-exif',
'--with-config-file-path=/etc/php55',
'--with-config-file-scan-dir=/etc/php55/conf.d',
'--with-mysql=/usr',
'--with-mysql-sock=/var/run/mysqld/mysqld.sock',
'--with-zlib',
'--with-gmp',
'--with-zlib-dir=/usr',
'--with-gettext',
'--with-kerberos',
'--with-imap-ssl',
'--with-mcrypt=/usr/local',
'--with-iconv',
'--enable-sockets',
'--with-openssl',
'--with-pspell',
'--with-pdo-mysql=mysqlnd',
'--with-pdo-sqlite',
'--with-pgsql',
'--with-pdo-pgsql',
'--enable-soap',
'--enable-xmlreader',
'--enable-phar=shared',
'--enable-opcache',
'--with-xsl',
'--enable-ftp',
'--enable-cgi',
'--with-curl=/usr',
'--with-tidy',
'--with-xmlrpc',
'--enable-mbstring',
'--enable-sysvsem',
'--enable-sysvshm',
'--enable-shmop',
'--with-readline',
'--enable-pcntl',
'--enable-fpm',
'--enable-intl',
'--with-imap',
'--with-mysqli=mysqlnd',
'--enable-calendar',
'--enable-bcmath'
];

$conf[54] = [
'--with-apxs2=/usr/bin/apxs2',
'--enable-zend-signals',
'--with-gd',
'--with-jpeg-dir=/usr',
'--with-png-dir=/usr',
'--with-vpx-dir=/usr',
'--with-freetype-dir=/usr',
'--with-t1lib=/usr',
'--enable-gd-native-ttf',
'--enable-exif',
'--with-mysql=/usr',
'--with-mysql-sock=/var/run/mysqld/mysqld.sock',
'--with-zlib',
'--with-gmp',
'--with-zlib-dir=/usr',
'--with-gettext',
'--with-kerberos',
'--with-imap-ssl',
'--with-mcrypt',
'--with-iconv',
'--enable-sockets',
'--with-openssl',
'--with-pspell',
'--with-pdo-mysql=mysqlnd',
'--with-pdo-sqlite',
'--with-pgsql',
'--with-pdo-pgsql',
'--enable-soap',
'--enable-xmlreader',
'--enable-phar=shared',
'--with-xsl',
'--enable-ftp',
'--enable-cgi',
'--with-curl=/usr',
'--with-tidy',
'--with-xmlrpc',
'--enable-mbstring',
'--enable-sysvsem',
'--enable-sysvshm',
'--enable-shmop',
'--with-readline',
'--enable-pcntl',
'--enable-fpm',
'--enable-intl',
'--with-imap',
'--with-mysqli=mysqlnd',
'--enable-calendar',
'--enable-bcmath'
];

function build_it($branch, $conf_flags) {
    $cmds = [
        "git checkout $branch",
        "git fetch --all 2>&1 1> /dev/null",
        "git reset --hard origin/$branch 2>&1 1> /dev/null",
        "git pull -r",
        "./vcsclean 2>&1 1> /dev/null",
        "./buildconf --force",
        "=configuring...",
        "./configure $conf_flags 2>&1 1>> /tmp/build.log",
        "=\ncompiling...",
        "make -j2 2>&1 1>> /tmp/build.log",
        "=\ninstalling...",
        "sudo make install 2>&1 1>> /tmp/build.log",
        "=\ndone\n"
    ];
    foreach($cmds as $cmd) {
        if($cmd[0]=='=') echo substr($cmd,1);
        else shell_exec($cmd);
    }
}

$dir = getcwd();
chdir("/home/vagrant/php-src");
shell_exec("truncate --size 0 /tmp/build.log");
echo "Build log in /tmp/build.log\n";

foreach($build as $v=>$b) {
    switch($v) {
        case 72:
            if(!$zts || $world) {
                echo "Building PHP 7.2\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --disable-debug --prefix=/usr/local/php72';
                build_it('master', $conf_flags);
                echo "Building PHP 7.2-debug\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --enable-debug --prefix=/usr/local/php72-debug';
                build_it('master', $conf_flags);
            }
            if($zts || $world) {
                echo "Building PHP 7.2-zts\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --disable-debug --enable-maintainer-zts --prefix=/usr/local/php72-zts';
                build_it('master', $conf_flags);
                echo "Building PHP 7.2-debug-zts\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --enable-debug --enable-maintainer-zts --prefix=/usr/local/php72-debug-zts';
                build_it('master', $conf_flags);
            }
            break;

        case 71:
            if(!$zts || $world) {
                echo "Building PHP 7.1\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --disable-debug --prefix=/usr/local/php71';
                build_it('PHP-7.1', $conf_flags);
                echo "Building PHP 7.1-debug\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --enable-debug --prefix=/usr/local/php71-debug';
                build_it('PHP-7.1', $conf_flags);
            }
            if($zts || $world) {
                echo "Building PHP 7.1-zts\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --disable-debug --enable-maintainer-zts --prefix=/usr/local/php71-zts';
                build_it('PHP-7.1', $conf_flags);
                echo "Building PHP 7.1-debug-zts\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --enable-debug --enable-maintainer-zts --prefix=/usr/local/php71-debug-zts';
                build_it('PHP-7.1', $conf_flags);
            }
            break;

        case 7:
            if(!$zts || $world) {
                echo "Building PHP 7.0\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --disable-debug --prefix=/usr/local/php70';
                build_it('PHP-7.0', $conf_flags);
                echo "Building PHP 7.0-debug\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --enable-debug --prefix=/usr/local/php70-debug';
                build_it('PHP-7.0', $conf_flags);
            }
            if($zts || $world) {
                echo "Building PHP 7.0-zts\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --disable-debug --enable-maintainer-zts --prefix=/usr/local/php70-zts';
                build_it('PHP-7.0', $conf_flags);
                echo "Building PHP 7.0-debug-zts\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --enable-debug --enable-maintainer-zts --prefix=/usr/local/php70-debug-zts';
                build_it('PHP-7.0', $conf_flags);
            }
            break;

        case 56:
            if(!$zts || $world) {
                echo "Building PHP 5.6\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --disable-debug --prefix=/usr/local/php56';
                build_it('PHP-5.6', $conf_flags);
                echo "Building PHP 5.6-debug\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --enable-debug --prefix=/usr/local/php56-debug';
                build_it('PHP-5.6', $conf_flags);
            }
            if($zts || $world) {
                echo "Building PHP 5.6-zts\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --disable-debug --enable-maintainer-zts --prefix=/usr/local/php56-zts';
                build_it('PHP-5.6', $conf_flags);
                echo "Building PHP 5.6-debug-zts\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --enable-debug --enable-maintainer-zts --prefix=/usr/local/php56-debug-zts';
                build_it('PHP-5.6', $conf_flags);
            }
            break;

        case 55:
            if(!$zts || $world) {
                echo "Building PHP 5.5\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --disable-debug --prefix=/usr/local/php55';
                build_it('PHP-5.5', $conf_flags);
                echo "Building PHP 5.5-debug\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --enable-debug --prefix=/usr/local/php55-debug';
                build_it('PHP-5.5', $conf_flags);
            }
            if($zts || $world) {
                echo "Building PHP 5.5-zts\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --disable-debug --enable-maintainer-zts --prefix=/usr/local/php55-zts';
                build_it('PHP-5.5', $conf_flags);
                echo "Building PHP 5.5-debug-zts\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --enable-debug --enable-maintainer-zts --prefix=/usr/local/php55-debug-zts';
                build_it('PHP-5.5', $conf_flags);
            }
            break;

        case 54:
            if(!$zts || $world) {
                echo "Building PHP 5.4\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --disable-debug --prefix=/usr/local/php54 --with-config-file-path=/etc/php54 --with-config-file-scan-dir=/etc/php54/conf.d';
                build_it('PHP-5.4', $conf_flags);
                echo "Building PHP 5.4-debug\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --enable-debug --prefix=/usr/local/php54-debug --with-config-file-path=/etc/php54-debug --with-config-file-scan-dir=/etc/php54-debug/conf.d';
                build_it('PHP-5.4', $conf_flags);
            }
            if($zts || $world) {
                echo "Building PHP 5.4-zts\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --disable-debug --enable-maintainer-zts --prefix=/usr/local/php54-zts --with-config-file-path=/etc/php54-zts --with-config-file-scan-dir=/etc/php54-zts/conf.d';
                build_it('PHP-5.4', $conf_flags);
                echo "Building PHP 5.4-debug-zts\n";
                $conf_flags = implode(' ', $conf[$v]) . ' --enable-debug --enable-maintainer-zts --prefix=/usr/local/php54-debug-zts --with-config-file-path=/etc/php54-debug-zts --with-config-file-scan-dir=/etc/php54-debug-zts/conf.d';
                build_it('PHP-5.4', $conf_flags);
            }
            break;
    }
}
